//
//  TopicListCell.m
//  QQplayground
//
//  Created by zhangxy on 2018/6/28.
//  Copyright © 2018年 zhang. All rights reserved.
//

#import "TopicListCell.h"
#import <SDWebImage/UIImageView+WebCache.h>

@implementation TopicListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    _imgBgView.layer.masksToBounds = YES;
    _imgBgView.layer.cornerRadius = 10.0f;
    _imgBgView.backgroundColor = [UIColor whiteColor];
    
    [_labAuthor setTextColor:UIColorFromRGB(BgTitleColor)];
    [_labAuthor setTextAlignment:NSTextAlignmentLeft];
    [_labAuthor setFont:FontThirteen];
    [_labTime setTextColor:UIColorFromRGB(TextSizeNineColor)];
    [_labTime setFont:FontThirteen];
    [_labTitle setTextColor:UIColorFromRGB(TextSizeOneColor)];
    [_labTitle setFont:FontFiftent];
    
    [_imgContent setFrame:CGRectMake(10, 10, ScreenWidth - 20, 180)];
    [ZZViewUtils cutViewRound:_imgContent radius:10.0f byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight];

}

-(void)dataToView:(SpecialEntity *) obj{
    self.backgroundColor = UIColor.clearColor;
    
    _imgContent.contentMode = UIViewContentModeScaleAspectFill;
    [_imgContent sd_setImageWithURL:[NSURL URLWithString:obj.image_url] placeholderImage:[UIImage imageNamed:@"default_image"]];
    [_labTitle setText:obj.title];
    [_labTime setText:obj.add_time];
    [_labAuthor setText:[NSString stringWithFormat:@"%@|%@",obj.nav_name,obj.author]];
    
    [self autoWidthOfLabelHegith:_labTitle with:ScreenWidth - 40];
    
    
    [self autoWidthOfLabel:_labTime with:21];
    CGRect timeF = _labTime.frame;
    timeF.origin.y = CGRectGetMaxY(_labTitle.frame)+5;
    _labTime.frame = timeF;
    
    [self autoWidthOfLabel:_labAuthor with:21];
    
    CGRect af = self.labAuthor.frame;
    af.origin.y = CGRectGetMaxY(_labTitle.frame)+5;
    af.origin.x = CGRectGetMaxX(timeF) + 5;
    self.labAuthor.frame = af;
    
    
    [_imgBgView setFrame:CGRectMake(10, 10, ScreenWidth - 20, CGRectGetMaxY(af) + 5)];
    
    [self setFrame:CGRectMake(0, 0, ScreenWidth, CGRectGetMaxY(_imgBgView.frame) + 10)];
}


/**
 计算Label高度
 
 @param label 要计算的label，设置了值
 @param width label的最大宽度
 */
- (CGSize )autoWidthOfLabel:(UILabel *)label with:(CGFloat )height{
    //Calculate the expected size based on the font and linebreak mode of your label
    // FLT_MAX here simply means no constraint in height
    CGSize maximumLabelSize = CGSizeMake(FLT_MAX, height);
    
    CGSize expectedLabelSize = [label sizeThatFits:maximumLabelSize];
    
    //adjust the label the the new height.
    CGRect newFrame = label.frame;
    newFrame.size.width = expectedLabelSize.width;
    label.frame = newFrame;
    [label updateConstraintsIfNeeded];
    
    return expectedLabelSize;
}

- (CGSize )autoWidthOfLabelHegith:(UILabel *)label with:(CGFloat )width{
    //Calculate the expected size based on the font and linebreak mode of your label
    // FLT_MAX here simply means no constraint in height
    CGSize maximumLabelSize = CGSizeMake(width, FLT_MAX);
    
    CGSize expectedLabelSize = [label sizeThatFits:maximumLabelSize];
    
    //adjust the label the the new height.
    CGRect newFrame = label.frame;
    newFrame.size.height = expectedLabelSize.height;
    label.frame = newFrame;
    [label updateConstraintsIfNeeded];
    
    return expectedLabelSize;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
